<?php

/**
 * 将unicode字符串按传入长度分割成数组
 * @param  string  $str 传入字符串
 * @param  integer $l   字符串长度
 * @return mixed      数组或false
 */
function str_split_unicode($str, $l = 0) {
    if ($l > 0) {
        $ret = array();
        $len = mb_strlen($str, "UTF-8");
        for ($i = 0; $i < $len; $i += $l) {
            $ret[] = mb_substr($str, $i, $l, "UTF-8");
        }
        return $ret;
    }
    return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
}

function buy360_tripall($vv){

    $vv = str_replace('――', '―', $vv);
    $vv = str_replace('(附光盘)', '', $vv);
    $vv = str_replace('（第三册）', '', $vv);
    $vv = str_replace('（第二册）', '', $vv);
	$vv = str_replace('[精装]', '', $vv);
	$vv = str_replace('[平装]', '', $vv);
    
	$vv = preg_replace('#\(.*\)#isU', '', $vv);
    $vv = preg_replace('#（.*）#isU', '', $vv);
    $vv = preg_replace('#【.*】#isU', '', $vv);
    $vv = preg_replace('#\(.*）#isU', '', $vv);
    $vv = preg_replace('#（.*\)#isU', '', $vv);

	$vv = str_replace('??', '', $vv);
    $vv = str_replace('&nbsp;', ' ', $vv);

	return $vv;

}

function ddsql_tripall($vv) {
    $vv = addslashes($vv);
    $vv = str_replace('――', '―', $vv);
    $vv = str_replace('(附光盘)', '', $vv);
    $vv = str_replace('（第三册）', '', $vv);
    $vv = str_replace('（第二册）', '', $vv);
    $vv = preg_replace('#\(.*\)#isU', '', $vv);
    $vv = preg_replace('#（.*）#isU', '', $vv);
    $vv = preg_replace('#【.*】#isU', '', $vv);
    $vv = preg_replace('#\(.*）#isU', '', $vv);
    $vv = preg_replace('#（.*\)#isU', '', $vv);
    $vv = preg_replace('#\,[0-9]+\,[0-9]+#', '', $vv);
	$vv = preg_replace('#\,[0-9]+#', '', $vv);
    $vv = str_replace('??', '', $vv);
    $vv = str_replace('&nbsp;', ' ', $vv);
    return $vv;
}

function getBookCover($categoryid, $bookid, $img = true) {
    if (!$img) {
        return HTTP_IMGCDN . '/bookcover/nopic.jpg';
    }
    return HTTP_IMGCDN . '/bookcover/' . $categoryid . '/' . substr(md5($bookid), 0, 2) . '/' . $bookid . '.jpg';
}

function setTaskCurrKey($flag, $key) {
    $t = debug();
    $dir = dirname($t['file']);
    $file = $dir . '/' . $flag . '.txt';
    file_put_contents($file, $key);
}

function getTaskCurrKey($flag) {
    $t = debug();
    $dir = dirname($t['file']);
    $file = $dir . '/' . $flag . '.txt';
    return file_get_contents($file);
}

//快速读写
function Fset($flag, $key) {
    $key = json_encode($key);
    $t = debug();
    $dir = dirname($t['file']);
    $file = $dir . '/data/fgs/' . $flag . '.txt';
    FcheckDir($file);
    file_put_contents($file, $key);
}

//快速读写
function Fget($flag) {
    $t = debug();
    $dir = dirname($t['file']);
    $file = $dir . '/data/fgs/' . $flag . '.txt';
    FcheckDir($file);
    return json_decode(file_get_contents($file), true);
}

//获取一个HTML文件里面的所有的class名称
function parseHtmlClass($file) {
    $s = file_get_contents($file);
    $p = '#class="(.*)"#isU';
    preg_match_all($p, $s, $d);
    return $d;
}

//获取一个CSS文件里面的所有的class名称
function parseCssClass($file) {
    $s = file_get_contents($file);
    $p = '#\.([_a-zA-Z]{1}[a-zA-Z\-_0-9]*\{)#isU';
    preg_match_all($p, $s, $d);
    $p = '#\.([_a-zA-Z]{1}[a-zA-Z_\-0-9]*,)#isU';
    preg_match_all($p, $s, $e);
    $p = '#\.([_a-zA-Z]{1}[a-zA-Z_\-0-9]*)\s#isU';
    preg_match_all($p, $s, $f);
    foreach ($d[1] as &$v) {
        $v = str_replace('{', '', $v);
    }unset($v);
    foreach ($e[1] as &$v) {
        $v = str_replace(',', '', $v);
    }unset($v);
    $R = array_merge($d[1], $f[1], $e[1]);
    sort($R);
    return $R;
}

#获取一个HTML文件里面的class="*"

function getClassDyFromHtml($file) {
    $s = file_get_contents($file);
    $p = '#class="(.*)"#isU';
    preg_match_all($p, $s, $d);
    $arrClass = array();
    foreach ($d[1] as $strClass) {
        $arrClass[] = 'class="' . $strClass . '"';
    }
    $d = array_filter(array_unique($arrClass));
    sort($d);
    return $d;
}

#获取一个HTML文件里面的所有class="*"

function getClassNameFromHtml($file) {
    $s = file_get_contents($file);
    $p = '#class="(.*)"#isU';
    preg_match_all($p, $s, $d);
    $arrClass = array();
    foreach ($d[1] as $strClass) {
        $arrT = explode(' ', $strClass);
        foreach ($arrT as $T) {
            $arrClass[] = $T;
        }
    }
    $d = array_filter(array_unique($arrClass));
    sort($d);
    return $d;
}

#获取一组HTML文件里面的Class名称

function getClassFromHtmlGroup($htmlfiles) {
    $htmlclass = array();

    foreach ($htmlfiles as $htmlfile) {
        $e = parseHtmlClass($htmlfile);
        foreach ($e[1] as $t) {
            $htmlclass[] = $t;
        }
    }

    $b = array();

    foreach ($htmlclass as $v) {
        $y = explode(' ', $v);
        foreach ($y as $u) {
            $b[] = $u;
        }
    }

    $bhtmlclass = array_filter(array_unique($b));
    $d = array_filter(array_unique($bhtmlclass));
    sort($d);
    return $d;
}

#获取一批CSS文件里面的Class名称

function getAllClassNameFromCssGruop($cssfiles) {
    $cssclass = array();
    foreach ($cssfiles as $cssfile) {
        $e = parseCssClass($cssfile);
        foreach ($e as $t) {
            $cssclass[] = $t;
        }
    }
    $d = array_filter(array_unique($cssclass));
    sort($d);
    return $d;
}

#获取一批HTML文件里面的Class属性

function getAllClassDyFromHtmlGruop($htmlfiles) {
    $htmlclass = array();
    foreach ($htmlfiles as $htmlfile) {
        $e = getClassDyFromHtml($htmlfile);
        foreach ($e as $t) {
            $htmlclass[] = $t;
        }
    }
    $d = array_filter(array_unique($htmlclass));
    sort($d);
    return $d;
}

function getFileTotalLine($file) {
    $var = 'a' . md5($file);
    if (isset($GLOBALS['varStore']['fileCount'][$var])) {
        return $GLOBALS['varStore']['fileCount'][$var];
    }
    logformat('NOTICE:[START]Stating File Rows:' . $file);
    global $arrGlobalRuntimeEnvVars;
    $cmd = $arrGlobalRuntimeEnvVars['cmd']['cat'] . ' ' . $file . ' | ' . $arrGlobalRuntimeEnvVars['cmd']['wc -l'];
	echo $cmd;
    $GLOBALS['varStore']['fileCount'][$var] = intval(`$cmd`);
    logformat('NOTICE:[END]File Rows Is:' . $GLOBALS['varStore']['fileCount'][$var]);
    return intval($GLOBALS['varStore']['fileCount'][$var]);
}

#获取一个随机的用户名

function getOneRandUserName() {
    $username = getRandLineFromOneFile(DATA_USER_RAND_LIST_TXT_FILE);
    if (in_array($username, $GLOBALS['varStore']['hasRegisterUser'])) {
        return getOneRandUserEmail();
    }
    return $username;
}

#获取一个随机的邮箱

function getOneRandUserEmail() {
    $emailbox = getRandLineFromOneFile(DATA_EMAIL_RAND_LIST_TXT_FILE);
    if (in_array($emailbox, $GLOBALS['varStore']['hasRegisterUser'])) {
        return getOneRandUserEmail();
    }
    return $emailbox;
}

#在一个文件里面随机抽取一行

function getRandLineFromOneFile($file) {
	echo getFileTotalLine($file);
    $intRandKey = mt_rand(0, 100 - 1);
    $obj = new SplFileObject($file, 'r');
    $obj->seek($intRandKey);
    $line = $obj->current();
    $line = trim($line);
    if (!$line) {
        logformat('WARNING:' . '|' . __FUNCTION__ . ' Rand Failed And Retrying...$intRandKey Is:' . $intRandKey);
        return getOneRandUserName();
    }
    logformat('NOTICE:Rand Success...$intRandKey:' . $intRandKey);
    return $line;
}

function getFileAllLines2Array() {
    
}

function _format_bytes($a_bytes) {
    if ($a_bytes < 1024) {
        return $a_bytes . ' B';
    } elseif ($a_bytes < 1048576) {
        return round($a_bytes / 1024, 2) . ' KB';
    } elseif ($a_bytes < 1073741824) {
        return round($a_bytes / 1048576, 2) . ' MB';
    } elseif ($a_bytes < 1099511627776) {
        return round($a_bytes / 1073741824, 2) . ' GB';
    } elseif ($a_bytes < 1125899906842624) {
        return round($a_bytes / 1099511627776, 2) . ' TB';
    } elseif ($a_bytes < 1152921504606846976) {
        return round($a_bytes / 1125899906842624, 2) . ' PB';
    } elseif ($a_bytes < 1180591620717411303424) {
        return round($a_bytes / 1152921504606846976, 2) . ' EB';
    } elseif ($a_bytes < 1208925819614629174706176) {
        return round($a_bytes / 1180591620717411303424, 2) . ' ZB';
    } else {
        return round($a_bytes / 1208925819614629174706176, 2) . ' YB';
    }
}

?>